package com.lottery.pay.Utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Map;

public class XMLPro {
	
	/**
	 * 传入一组键值对，返回一个简单的xml格式字符串
	 * @param map
	 * @return
	 * @author 邢超
	 * 创建时间：2017-2-27
	 *
	 */
	public static String toXml(Map<String,String> map){
		StringBuilder sb = new StringBuilder();
		sb.append("<xml>");
		for(String key:map.keySet()){
			sb.append(toTab(key, map.get(key)));
		}
		sb.append("</xml>");
		return sb.toString();
	}
	
	
	/**
	 * 传入key和value 返回被标记包装后的字符串;
	 * @param key
	 * @param value
	 * @return
	 * @author 邢超
	 * 创建时间：2017-2-27
	 *
	 */
	public static String toTab(String key, String value){
		StringBuilder sb = new StringBuilder();
		sb.append("<"+key+">");
		sb.append(value);
		sb.append("</"+key+">");
		return sb.toString();
	}
	
	
	/**
	 * 返回被<![CDATA[ ]]>包裹的数据
	 * @param str
	 * @return
	 * @author 邢超
	 * 创建时间：2017-4-25
	 *
	 */
	public static String toXmlCDATA(String str){
		return "<![CDATA["+str+"]]>";
	}
	
	
	
	/**
	 * 返回被<![CDATA[ ]]>包裹的数据
	 * @param str
	 * @return
	 * @author 邢超
	 * 创建时间：2017-4-25
	 *
	 */
	public static Map<String,String> toXmlCDATA(Map<String,String> map){
		 for(String key:map.keySet()){
			 map.put(key, "<![CDATA["+map.get(key)+"]]>");
		 }
		
		return map;
	}
	
	
	
	
	
	
	public static Element getEle(String xml){
		try {
			Document doc = DocumentHelper.parseText(xml);
			Element ele = doc.getRootElement();
			return ele;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static Map<String,Object> getMap(String xml) throws DocumentException{
		Document doc = DocumentHelper.parseText(xml);
		Element ele = doc.getRootElement();
		return null;
	}
	
	public static void main(String[] args) throws DocumentException {
	/*	String xml = "<xml>" +
				"<ToUserName><![CDATA[toUser]]></ToUserName>" +
				"<FromUserName><![CDATA[fromUser]]></FromUserName>" +
				"<CreateTime>12345678</CreateTime>" +
				"<MsgType><![CDATA[text]]></MsgType>" +
				"<Content><![CDATA[你好]]></Content>" +"</xml>";
		
		getMap(xml);*/
		
		
		System.out.println(toTab("abc", "哈哈哈"));
		
	}

}
